public class 二分查找 {
    public static int binarySearch(int[] arr,int n) {
        int l = 0;
        int r = arr.length-1;
        int mid = 0;
        while(l<=r) {
            mid = (l+r)/2;
            if(arr[mid]==n) {
                return n;
            } else if (arr[mid]<n) {
                l = mid+1;
            }else {
                r = mid-1;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        //
        int[] arr = {1,2,3,4,5,6,7,8};
        int n = 3;
        int i = binarySearch(arr,n);

        System.out.println(n+"的小标为："+i);
        Iphone iphone = new Iphone();

    }
}
